/**
 * Created by hehe on 2014-08-14.
 */
var http = require('http'),
    express = require('express'),
    bodyParser = require('body-parser'),
    app = express();
app.set('port', '3000');
app.use(express.static('./public'));
app.use(bodyParser.json());
var request=require('request');
var signature=require("./signature");
app.get('/',function(req,res){
    return res.redirect('/start.html');
})

app.use('/SimpleRequest',function (req, res) {
    var options = req.body;
    options.headers==undefined ? options.headers={"Content-Type": "application/json"}:"";
    if(req.headers.cookie && !options.headers.cookie)
        options.headers.cookie = req.headers.cookie;

    if(options.encrypt){//验证是否需要加密
        if(options.key==undefined){
            options.key="";
        }
        if(!options.data.signature)
        options.data.signature=signature.signature(options.data,options.key);
    }
    var reqParams = {
        uri:options.url,
        headers:options.headers,
        method:options.method,
        body: JSON.stringify(options.data)};
    console.log(reqParams);
    request(reqParams,function(err,data,body){
        var headers = data.headers;

        res.status(data.statusCode);
        for(var key in headers) {
            res.setHeader(key, headers[key]);
        }
        res.send(body);

    });
});



http.createServer(app).listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port'));
});



process.on('uncaughtException', function (err) {
    console.log(err);
});




